<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>[配置文件] toml配置和使用 - Never Give Up</title>
  <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="ZhangKQ" /><meta name="description" content="toml配置和使用 一.简介 TOML是前GitHub CEO， Tom Preston-Werner，于2013年创建的语言，其目标是成为一个小规模的易" /><meta name="keywords" content="开发语言, 配置文件, toml" />






<meta name="generator" content="Hugo 0.92.0 with theme even" />


<link rel="canonical" href="https://blog.nevergiveup.tech/post/dev_language/conf/toml%E9%85%8D%E7%BD%AE%E5%92%8C%E4%BD%BF%E7%94%A8/" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">



<link href="/sass/main.min.b5a744db6de49a86cadafb3b70f555ab443f83c307a483402259e94726b045ff.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.css" integrity="sha256-7TyXnr2YU040zfSP+rEcz29ggW4j56/ujTPwjMzyqFY=" crossorigin="anonymous">


<meta property="og:title" content="[配置文件] toml配置和使用" />
<meta property="og:description" content="toml配置和使用 一.简介 TOML是前GitHub CEO， Tom Preston-Werner，于2013年创建的语言，其目标是成为一个小规模的易" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://blog.nevergiveup.tech/post/dev_language/conf/toml%E9%85%8D%E7%BD%AE%E5%92%8C%E4%BD%BF%E7%94%A8/" /><meta property="article:section" content="post" />
<meta property="article:published_time" content="2021-12-23T15:37:56+08:00" />
<meta property="article:modified_time" content="2022-02-10T16:37:56+08:00" />

<meta itemprop="name" content="[配置文件] toml配置和使用">
<meta itemprop="description" content="toml配置和使用 一.简介 TOML是前GitHub CEO， Tom Preston-Werner，于2013年创建的语言，其目标是成为一个小规模的易"><meta itemprop="datePublished" content="2021-12-23T15:37:56+08:00" />
<meta itemprop="dateModified" content="2022-02-10T16:37:56+08:00" />
<meta itemprop="wordCount" content="740">
<meta itemprop="keywords" content="开发语言,配置文件,toml," /><meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="[配置文件] toml配置和使用"/>
<meta name="twitter:description" content="toml配置和使用 一.简介 TOML是前GitHub CEO， Tom Preston-Werner，于2013年创建的语言，其目标是成为一个小规模的易"/>

<!--[if lte IE 9]>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
<![endif]-->

<!--[if lt IE 9]>
  <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->

</head>
<body>
  <div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/" class="logo">Never Give Up</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    <a href="/">
        <li class="mobile-menu-item">主页</li>
      </a><a href="/post/">
        <li class="mobile-menu-item">归档</li>
      </a><a href="/tags/">
        <li class="mobile-menu-item">标签</li>
      </a><a href="/categories/">
        <li class="mobile-menu-item">分类</li>
      </a><a href="/remark/">
        <li class="mobile-menu-item">随言碎语</li>
      </a><a href="/about/">
        <li class="mobile-menu-item">关于</li>
      </a>
  </ul>

  


</nav>

  <div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/" class="logo">Never Give Up</a>
</div>





<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
        <a class="menu-item-link" href="/">主页</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/post/">归档</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/tags/">标签</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/categories/">分类</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/remark/">随言碎语</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/about/">关于</a>
      </li>
  </ul>
</nav>

    </header>

    <main id="main" class="main">
      <div class="content-wrapper">
        <div id="content" class="content">
          <article class="post">
    
    <header class="post-header">
      <h1 class="post-title">[配置文件] toml配置和使用</h1>

      <div class="post-meta">
        <span class="post-time"> 2021-12-23 </span>
        <div class="post-category">
            <a href="/categories/%E5%BC%80%E5%8F%91%E8%AF%AD%E8%A8%80/"> 开发语言 </a>
            </div>
          <span class="more-meta"> 约 740 字 </span>
          <span class="more-meta"> 预计阅读 2 分钟 </span>
        
      </div>
    </header>

    <div class="post-toc" id="post-toc">
  <h2 class="post-toc-title">文章目录</h2>
  <div class="post-toc-content always-active">
    <nav id="TableOfContents">
  <ul>
    <li>
      <ul>
        <li><a href="#toml配置和使用">toml配置和使用</a>
          <ul>
            <li><a href="#一简介">一.简介</a></li>
            <li><a href="#二示例">二.示例</a></li>
            <li><a href="#三语法">三.语法</a></li>
            <li><a href="#四go包">四.go包</a></li>
          </ul>
        </li>
      </ul>
    </li>
  </ul>
</nav>
  </div>
</div>
    <div class="post-content">
      <h2 id="toml配置和使用">toml配置和使用</h2>
<h3 id="一简介">一.简介</h3>
<blockquote>
<p>TOML是前GitHub CEO， Tom Preston-Werner，于2013年创建的语言，其目标是成为一个小规模的易于使用的语义化配置文件格式。TOML被设计为可以无二义性的转换为一个哈希表(Hash table)。TOML是大小写敏感的，必须是UTF-8编码</p>
</blockquote>
<h3 id="二示例">二.示例</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-toml" data-lang="toml"><span class="nx">title</span> <span class="p">=</span> <span class="s2">&#34;TOML Example&#34;</span>

<span class="p">[</span><span class="nx">owner</span><span class="p">]</span>
<span class="nx">name</span> <span class="p">=</span> <span class="s2">&#34;Lance Uppercut&#34;</span>
<span class="nx">dob</span> <span class="p">=</span> <span class="ld">1979-05-27T07:32:00-08:00</span> <span class="c"># 日期是一等公民</span>

<span class="p">[</span><span class="nx">database</span><span class="p">]</span>
<span class="nx">server</span> <span class="p">=</span> <span class="s2">&#34;192.168.1.1&#34;</span>
<span class="nx">ports</span> <span class="p">=</span> <span class="p">[</span> <span class="mi">8001</span><span class="p">,</span> <span class="mi">8001</span><span class="p">,</span> <span class="mi">8002</span> <span class="p">]</span>
<span class="nx">connection_max</span> <span class="p">=</span> <span class="mi">5000</span>
<span class="nx">enabled</span> <span class="p">=</span> <span class="kc">true</span>

<span class="p">[</span><span class="nx">servers</span><span class="p">]</span>
  <span class="c">#你可以使用空格、制表符进行缩进，或者根本不缩进。TOML不关心缩进。</span>
  <span class="p">[</span><span class="nx">servers</span><span class="p">.</span><span class="nx">alpha</span><span class="p">]</span>
  <span class="nx">ip</span> <span class="p">=</span> <span class="s2">&#34;10.0.0.1&#34;</span>
  <span class="nx">dc</span> <span class="p">=</span> <span class="s2">&#34;eqdc10&#34;</span>

  <span class="p">[</span><span class="nx">servers</span><span class="p">.</span><span class="nx">beta</span><span class="p">]</span>
  <span class="nx">ip</span> <span class="p">=</span> <span class="s2">&#34;10.0.0.2&#34;</span>
  <span class="nx">dc</span> <span class="p">=</span> <span class="s2">&#34;eqdc10&#34;</span>

<span class="p">[</span><span class="nx">clients</span><span class="p">]</span>
<span class="nx">data</span> <span class="p">=</span> <span class="p">[</span> <span class="p">[</span><span class="s2">&#34;gamma&#34;</span><span class="p">,</span> <span class="s2">&#34;delta&#34;</span><span class="p">],</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">]</span> <span class="p">]</span>

<span class="c"># 数组内可以混入换行符</span>
<span class="nx">hosts</span> <span class="p">=</span> <span class="p">[</span>
  <span class="s2">&#34;alpha&#34;</span><span class="p">,</span>
  <span class="s2">&#34;omega&#34;</span>
<span class="p">]</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="三语法">三.语法</h3>
<h4 id="1注释">1.注释</h4>
<p>使用#来表示注释开始，至当前行尾结束。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-toml" data-lang="toml"><span class="c"># I am a comment. Hear me roar. Roar.</span>
</code></pre></td></tr></table>
</div>
</div><h4 id="2字符串">2.字符串</h4>
<p>TOML中有4种字符串表示方法：基本、多行-基本、字面量、多行-字面量</p>
<h5 id="21-基本字符串">2.1 基本字符串</h5>
<p>由双引号包裹，所有Unicode字符均可出现，除了双引号、反斜线、控制字符(U+0000 to U+001F)需要转义。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-toml" data-lang="toml"><span class="nx">str</span> <span class="p">=</span> <span class="s2">&#34;I&#39;m a string. \&#34;You can quote me\&#34;.&#34;</span>
</code></pre></td></tr></table>
</div>
</div><h5 id="22-多行-基本字符串">2.2 多行-基本字符串</h5>
<p>由三个双引号包裹，除了分隔符开始的换行外，字符串内的换行将被保留</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-toml" data-lang="toml"><span class="nx">str1</span> <span class="p">=</span> <span class="s2">&#34;&#34;&#34;
</span><span class="s2">Roses are red
</span><span class="s2">Violets are blue&#34;&#34;&#34;</span>
</code></pre></td></tr></table>
</div>
</div><h5 id="23-字面量字符串">2.3 字面量字符串</h5>
<p>由单引号包裹，其内不允许转义，因此可以方便的表示基本字符串中需要转义的内容</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-toml" data-lang="toml"><span class="nx">winpath</span> <span class="p">=</span> <span class="s1">&#39;C:\Users\nodejs\templates&#39;</span>
</code></pre></td></tr></table>
</div>
</div><h5 id="24-多行-字面量字符串">2.4 多行-字面量字符串</h5>
<p>与多行-基本字符串相似</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-toml" data-lang="toml"><span class="nx">str1</span> <span class="p">=</span> <span class="s1">&#39;&#39;&#39;
</span><span class="s1">Roses are red
</span><span class="s1">Violets are blue&#39;&#39;&#39;</span>
</code></pre></td></tr></table>
</div>
</div><h4 id="3数值与bool值">3.数值与BOOL值</h4>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-toml" data-lang="toml"><span class="nx">int1</span> <span class="p">=</span> <span class="mi">+99</span>
<span class="nx">flt3</span> <span class="p">=</span> <span class="mf">-0.01</span>
<span class="nx">bool1</span> <span class="p">=</span> <span class="kc">true</span>
</code></pre></td></tr></table>
</div>
</div><h4 id="4日期时间">4.日期时间</h4>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-toml" data-lang="toml"><span class="nx">date1</span> <span class="p">=</span> <span class="ld">1979-05-27T07:32:00Z</span>
</code></pre></td></tr></table>
</div>
</div><h4 id="5数组">5.数组</h4>
<p>数组使用方括号包裹。空格会被忽略，包括换行符。元素使用逗号分隔。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-toml" data-lang="toml"><span class="nx">arr1</span> <span class="p">=</span> <span class="p">[</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span> <span class="p">]</span>
<span class="nx">arr2</span> <span class="p">=</span> <span class="p">[</span> <span class="s2">&#34;red&#34;</span><span class="p">,</span> <span class="s2">&#34;yellow&#34;</span><span class="p">,</span> <span class="s2">&#34;green&#34;</span> <span class="p">]</span>
<span class="nx">arr3</span> <span class="p">=</span> <span class="p">[</span> <span class="p">[</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span> <span class="p">],</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">]</span> <span class="p">]</span>
</code></pre></td></tr></table>
</div>
</div><h4 id="6表格">6.表格</h4>
<p>表格叶称为哈希表或字典，用来存储键值对。表格名由方括号包裹，且自成一行。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-toml" data-lang="toml"><span class="p">[</span><span class="nx">dog</span><span class="p">]</span>
<span class="nx">onekey</span> <span class="p">=</span> <span class="nx">onevalue</span>

<span class="p">[</span><span class="nx">dog</span><span class="p">.</span><span class="nx">tater</span><span class="p">]</span>
<span class="nx">type</span> <span class="p">=</span> <span class="s2">&#34;pug&#34;</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="四go包">四.go包</h3>
<p>在go中使用toml</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-golang" data-lang="golang"><span class="k">go</span> <span class="nx">get</span> <span class="nx">github</span><span class="p">.</span><span class="nx">com</span><span class="o">/</span><span class="nx">bbangert</span><span class="o">/</span><span class="nx">toml</span>
</code></pre></td></tr></table>
</div>
</div>
    </div>

    <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title">文章作者</span>
    <span class="item-content">ZhangKQ</span>
  </p>
  <p class="copyright-item">
    <span class="item-title">上次更新</span>
    <span class="item-content">
        2022-02-10
        
    </span>
  </p>
  
  <p class="copyright-item">
    <span class="item-title">许可协议</span>
    <span class="item-content"><a rel="license noopener" href="https://en.wikipedia.org/wiki/Wikipedia:Text_of_Creative_Commons_Attribution-ShareAlike_3.0_Unported_License" target="_blank">Creative Commons Attribution-ShareAlike License</a></span>
  </p>
</div>
<div class="post-reward">
  <input type="checkbox" name="reward" id="reward" hidden />
  <label class="reward-button" for="reward">赞赏支持</label>
  <div class="qr-code">
    
    <label class="qr-code-image" for="reward">
        <img class="image" src="/qrcode/wechat-qr-code.jpg">
        <span>微信打赏</span>
      </label>
    <label class="qr-code-image" for="reward">
        <img class="image" src="/qrcode/alipay-qr-code.jpg">
        <span>支付宝打赏</span>
      </label>
  </div>
</div><footer class="post-footer">
      <div class="post-tags">
          <a href="/tags/%E5%BC%80%E5%8F%91%E8%AF%AD%E8%A8%80/">开发语言</a>
          <a href="/tags/%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6/">配置文件</a>
          <a href="/tags/toml/">toml</a>
          </div>
      <nav class="post-nav">
        <a class="prev" href="/post/middleware/etcd%E9%9B%86%E7%BE%A4%E9%83%A8%E7%BD%B2%E4%B8%8E%E4%BD%BF%E7%94%A8/">
            <i class="iconfont icon-left"></i>
            <span class="prev-text nav-default">[etcd] etcd集群配置与使用</span>
            <span class="prev-text nav-mobile">上一篇</span>
          </a>
        <a class="next" href="/post/cloud_app/aliyun/%E9%98%BF%E9%87%8C%E4%BA%91-%E4%BA%91%E5%AD%98%E5%82%A8-%E5%AA%92%E4%BD%93%E5%A4%84%E7%90%86%E7%AD%89%E9%85%8D%E7%BD%AE%E6%B5%81%E7%A8%8B/">
            <span class="next-text nav-default">阿里云-云存储-媒体处理等配置流程</span>
            <span class="next-text nav-mobile">下一篇</span>
            <i class="iconfont icon-right"></i>
          </a>
      </nav>
    </footer>
  </article>
        </div>
        

  

  

      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="social-links">
      <a href="mailto:wdyxzkq@163.com" class="iconfont icon-email" title="email"></a>
      <a href="https://github.com/dysoso" class="iconfont icon-github" title="github"></a>
      <a href="https://gitee.com/dysoso" class="iconfont icon-gitlab" title="gitlab"></a>
  <a href="https://blog.nevergiveup.tech/index.xml" type="application/rss+xml" class="iconfont icon-rss" title="rss"></a>
</div>

<div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" href="https://blog.nevergiveup.tech/">blog.nevergiveup.tech</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 - 
    <a class="theme-link" href="https://github.com/olOwOlo/hugo-theme-even">Even</a>
  </span>

  

  <span class="copyright-year">
    &copy; 
    2021 - 
    2022<span class="heart"><i class="iconfont icon-heart"></i></span><span><a href="https://beian.miit.gov.cn/">蜀ICP备2021005948号-1</a></span>
  </span>
</div>

    </footer>

    <div class="back-to-top" id="back-to-top">
      <i class="iconfont icon-up"></i>
    </div>
  </div>
  
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/slideout@1.0.1/dist/slideout.min.js" integrity="sha256-t+zJ/g8/KXIJMjSVQdnibt4dlaDxc9zXr/9oNPeWqdg=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.js" integrity="sha256-XVLffZaxoWfGUEbdzuLi7pwaUJv1cecsQJQqGLe7axY=" crossorigin="anonymous"></script>



<script type="text/javascript" src="/js/main.min.c99b103c33d1539acf3025e1913697534542c4a5aa5af0ccc20475ed2863603b.js"></script>


<script type="application/javascript">
var doNotTrack = false;
if (!doNotTrack) {
	window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
	ga('create', 'never-give-up', 'auto');
	ga('set', 'anonymizeIp', true);
	ga('send', 'pageview');
}
</script>
<script async src='https://www.google-analytics.com/analytics.js'></script>







</body>
</html>
